package com.example.financialsystem;

import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

import com.google.android.material.appbar.CollapsingToolbarLayout;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.rengwuxian.materialedittext.MaterialEditText;

import org.apache.commons.lang3.StringUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import models.User;
import models.UserVO;
import okhttp3.Request;
import okhttp3.RequestBody;
import others.ServerResponse;
import utils.GsonUtil;
import utils.OkHttpCallback;
import utils.OkhttpUtil;

import static others.Const.CURRENT_USER;
import static others.Const.IP;
import static others.Const.JSON;
import static others.Const.PORT;
import static others.Const.currentUserVo;
import static others.Tools.ToastShow;
import static utils.GsonUtil.String2UserVoServerResponse;

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {

    @BindView(R.id.registerSubmit)
    FloatingActionButton buttonSubmit;
    @BindView(R.id.registerUsername)
    MaterialEditText editUsername;
    @BindView(R.id.registerPassword)
    MaterialEditText editPassword;
    @BindView(R.id.registerTel)
    MaterialEditText editTel;
    @BindView(R.id.registerEmail)
    MaterialEditText editEmail;
    @BindView(R.id.registerCollapsingToolbar)
    CollapsingToolbarLayout registerCollapsingToolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        ButterKnife.bind(this);
        buttonSubmit.setOnClickListener(this);
        registerCollapsingToolbar.setTitle("欢迎注册新账号");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.registerSubmit:
                String username = editUsername.getText().toString();
                String password = editPassword.getText().toString();
                String tel = editTel.getText().toString();
                String email = editEmail.getText().toString();
                if (!StringUtils.isBlank(username) && !StringUtils.isBlank(password)
                        && !StringUtils.isBlank(tel) && !StringUtils.isBlank(email)) {
                    User registerUser = new User(username, password, tel, email);
                    sendRequest(registerUser);
                } else {
                    ToastShow("请按要求填写注册信息");
                }
                break;
            default:
                break;
        }
    }

    public void sendRequest(User user) {
        String jsonData = GsonUtil.class2Json(user);
        String url = "http://" + IP + ":" + PORT + "/portal/user/register.do";
        RequestBody requestBody = RequestBody.create(JSON, jsonData);
        final Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkhttpUtil.SendRequestWithPost(request,
                            new OkHttpCallback() {
                                @Override
                                public void onFinish(String status, String responseData) {
                                    if (status.equals("Success")) {
                                        ServerResponse<UserVO> serverResponse = String2UserVoServerResponse(responseData);
                                        if (serverResponse.getStatus() == 0) {
                                            Intent intent = new Intent(RegisterActivity.this, HomeScreenActivity.class);
                                            intent.putExtra(CURRENT_USER, serverResponse.getData());
                                            currentUserVo = serverResponse.getData();
                                            startActivity(intent);
                                            finish();
                                        } else {
                                            Looper.prepare();
                                            ToastShow(serverResponse.getMsg());
                                            Looper.loop();
                                        }
                                    } else {
                                        Looper.prepare();
                                        ToastShow("注册失败，请稍后重试！");
                                        Looper.loop();
                                    }
                                }
                            });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
